<template>
<view class="decorate_page_container">
  <block>
    <custom-page-index-component id="indexPage" :receiveData="partialsName" v-if="partialsName"></custom-page-index-component>
  </block>
</view>
</template>

<script>


export default {
  data() {
    return {
      partials: null,
      richPage: null,
      sysWidth: 320 //图片大小
      ,

      /**
       * 生命周期函数--监听页面加载
       */
      onloadOpt: {},
      partialsName: "",
      article_article: ""
    };
  },

  props: {},
  onLoad: function (options) {
    console.log("===options==", options);

    if (!options) {
      console.log('-------noOption------');
      return;
    }

    if (!options.Cpage) {
      return;
    }

    this.onloadOpt = options;
    this.setData({
      partialsName: options.Cpage
    }); // this.getCustomPage(options.Cpage, options)    

    this.setData({
      sysWidth: getApp().globalData.sysWidth
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //this.chan()
    var article = '<div style="color:red">我是<br>HTML代码</div>';
    /**
    * WxParse.wxParse(bindName , type, data, target,imagePadding)
    * 1.bindName绑定的数据名(必填)
    * 2.type可以为html或者md(必填)
    * 3.data为传入的具体数据(必填)
    * 4.target为Page对象,一般为this(必填)
    * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
    */
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.refreshPage()
      wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('===onReachBottom====');
    var that = this;
    that.selectComponent("#indexPage").onReachBottom();
  },

  /**
   * 用户点击右上角分享
   */
  onShareTimeline:function(){
    return this.shareFun();
  },
  onShareAppMessage: function (res) {
    console.log(res);
    return this.shareFun();
  },
  methods: {
    refreshPage: function () {
      let that = this;
      console.log("首页刷新");
      wx.showToast({
        title: '加载中...',
        icon: 'none',
        duration: 500
      });
      that.setData({
        partialsName: null
      });
      setTimeout(function () {
        that.setData({
          partialsName: that.onloadOpt.Cpage
        });
      }, 500);
    },
    shareFun:function(){
        let that = this;
        let indexData = that.selectComponent("#indexPage").data.renderData;
        console.log("====indexData====", indexData);
        let params = {
          Cpage: indexData.channelName
        };
        let shareName = indexData.channelTitle;
        let shareAppMessageData = getApp().globalData.shareForFx2('custom_page', shareName, params, '', 'Cpage');
        console.log('params:', params);
        return shareAppMessageData;
    },
    chan: function () {
      var that = this;
      wxParse.wxParse('article', 'html', that.test, that, 0);
    },
    getPartials: function (partials) {
      var PaiXuPartials = [];
      var that = this;

      for (let i = 0; i < partials.length; i++) {
        if (typeof partials[i].jsonData == "string") {
          partials[i].jsonData = JSON.parse(partials[i].jsonData);
        }

        if (partials[i].partialType == 1) {
          //WxParse.wxParse('article', 'html', partials[i].jsonData.content, that, 5)
          setTimeout(function () {
            that.article_article = partials[i].jsonData.content;
          });
        }

        if (partials[i].partialType == 12) {
          wx.setNavigationBarTitle({
            title: partials[i].jsonData.title
          });

          if (!partials[i].jsonData.titleColor) {
            partials[i].jsonData.titleColor = '#000000';
          }

          if (!partials[i].jsonData.bgColor) {
            partials[i].jsonData.bgColor = '#ffffff';
          }

          console.log('setTitle-' + typeof partials[i].jsonData.titleColor);
          wx.setNavigationBarColor({
            frontColor: partials[i].jsonData.titleColor,
            backgroundColor: partials[i].jsonData.bgColor
          });
        } else {
          PaiXuPartials.push(partials[i]);
        }
      }

      this.setData({
        partials: PaiXuPartials
      });
      console.log(PaiXuPartials);
    },
    getCustomPage: function (Cpage, pageParam) {
      var finnalUrl = "/custom_page_" + Cpage + ".html";
      console.log(finnalUrl);
      var customIndex = getApp().globalData.AddClientUrl(finnalUrl, pageParam, 'get', 1);
      var that = this; // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true); //拿custom_page

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log("====custom_page=====", res);

          if (res.data.errcode < 0) {
            console.log(res.data.errMsg);
          } else {
            // 数据经过更改导航栏的名称更改在获取的数据中
            wx.setNavigationBarTitle({
              title: res.data.channelTitle
            }); //  导航栏的背景颜色根据app.setting.platformSetting里面拿取

            if (getApp().globalData.setting.platformSetting.defaultColor == '') {
              wx.setNavigationBarColor({
                frontColor: '#ffffff',
                backgroundColor: '#000000'
              });
            } else {
              wx.setNavigationBarColor({
                frontColor: '#ffffff',
                backgroundColor: getApp().globalData.setting.platformSetting.defaultColor
              });
            }

            wx.hideLoading();

            if (!!res.data.partials) {
              that.getPartials(res.data.partials);
            } else {
              console.log('--------error --------' + res.data);
            }
          }
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>